package com.bacon.gmall.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.bacon.gmall.common.pojo.GmallCorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2021-01-09 16:40
 * @description: 发送普通消息
 */

@Component
public class RabbitUtil {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    //发普通消息
    public void sendMessage(String exchange, String routingKey, Object message) {
        GmallCorrelationData gmallCorrelationData = assembly(exchange, routingKey, message);

        //在此将发送  Redis 中
        // Map  rabbit:message = new HashMap();
        //rabbit:message.put(k,v)
        redisTemplate.opsForHash().put(
                "rabbit:message", gmallCorrelationData.getId(), JSONObject.toJSONString(gmallCorrelationData));

        //发消息
        rabbitTemplate.convertAndSend(
                exchange, routingKey,
                message, gmallCorrelationData);
    }

    //发送延迟消息
    public void sendDeadLetterMessage(String exchange, String routingKey, Object msg, Integer delay) {

        GmallCorrelationData gmallCorrelationData = assembly(exchange, routingKey, msg);
        //是否设置延迟消息
        gmallCorrelationData.setDelay(true);
        gmallCorrelationData.setDelayTime(delay);

        //在此将发送  Redis中
        // Map  rabbit:message = new HashMap();
        //rabbit:message.put(k,v)
        redisTemplate.opsForHash().put(
                "rabbit:message", gmallCorrelationData.getId(), JSONObject.toJSONString(gmallCorrelationData));

        rabbitTemplate.convertAndSend(
                exchange,
                routingKey,
                msg,
                message -> {
                    message.getMessageProperties().setExpiration(String.valueOf(delay * 1000));
                    System.out.println("发送延迟消息时间 ： " + new Date());
                    return message;
                },
                gmallCorrelationData);

    }

    //基于插件的延迟消息
    public void sendDelayMessage(String exchange, String routingKey, String msg, int delay) {
        GmallCorrelationData gmallCorrelationData = assembly(exchange, routingKey, msg);
        //是否设置延迟消息
        gmallCorrelationData.setDelay(true);
        gmallCorrelationData.setDelayTime(delay);

        //在此将发送  Redis中
        // Map  rabbit:message = new HashMap();
        //rabbit:message.put(k,v)
        redisTemplate.opsForHash().put(
                "rabbit:message",
                gmallCorrelationData.getId(),
                JSONObject.toJSONString(gmallCorrelationData));

        rabbitTemplate.convertAndSend(
                exchange,
                routingKey,
                msg,
                message -> {
                    message.getMessageProperties().setDelay(delay * 1000);
                    System.out.println("发送延迟消息时间 ： " + new Date());
                    return message;
                },
                gmallCorrelationData);
    }

    //装配数据
    public GmallCorrelationData assembly(String exchange, String routingKey, Object message) {
        //子对象  消息丢失 回调下面这个对象进行重新发送
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        //ID  UUID
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        //交换机
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingKey);
        gmallCorrelationData.setMessage(message);

        return gmallCorrelationData;
    }



}
